import {
    reqCancelOrder,
    reqCreateUser,
    reqGetUserInfoOfToken,
    reqGetUserOrdersOfToken,
    reqLoginUser, reqPayOrder,
    reqUpdateOrderInfo, reqUpdateUserBalance
} from "@/api/index";

const state = {
    userBalance:"",
    userOrders:{}
};
const actions = {
    async createUser({commit}, data){
        let result = await reqCreateUser(data);
        if (result.code == 200){
            alert("注册成功");
        }else {
            alert(result.message);
        }
    },
    async loginUser({commit}, data){
        let result = await reqLoginUser(data);
        if (result.code == 200){
            window.localStorage.setItem("TOKEN", result.data.token);
            alert("登录成功");
            this.dispatch("detailPageGetUserInfo");
        }else {
            alert("账号密码不一致");
        }
    },
    async detailPageGetUserInfo({commit}){
        let result = await reqGetUserInfoOfToken();
        if (result.code == 200){
            const {userId, userName, userPhone, userPicture, userBalance} = result.data;
            window.localStorage.setItem("USERINFO", JSON.stringify({userId, userName, userPhone, userPicture}));
            commit("SETUSERBALANCE", userBalance);
        }
    },
    async getUserOrdersOfToken({commit}){
        console.log("开始获取userOrders");
        let result = await reqGetUserOrdersOfToken();
        if (result.code == 200){
            commit("GETUSERORDERSOFTOKEN", result.data);
        }
    },
    async updateOrder({commit}, data){
        let result = await reqUpdateOrderInfo(data);
        if (result.code == 200){
            commit("GETUSERORDERSOFTOKEN", result.data);
            alert("提交成功");
        }
    },
    async updataUserBalance({commit}, data) {
        let result = await reqUpdateUserBalance(data);
        if (result.code==200){
            alert("充值成功！");
            this.dispatch("detailPageGetUserInfo");
            this.$router.push("orders");
        }
    },
    // 支付订单
    async userPayOrder({commit}, data){
        let result = await reqPayOrder(data);
        if (result.code==200){
            alert('订单支付成功！');
            this.dispatch("detailPageGetUserInfo");
            this.dispatch('getUserOrdersOfToken');
        }else{
            alert(result.message);
        }
    },
    // 取消订单并完成退款
    async userCancelOrder({commit}, data){
        let result = await reqCancelOrder(data);
        if (result.code==200){
            alert('订单取消成功！');
            this.dispatch("detailPageGetUserInfo");
            this.dispatch('getUserOrdersOfToken');
        }else{
            alert(result.message);
        }
    },
    //上传头像
    uploadPicture(){

    }
};
const mutations = {
    GETUSERORDERSOFTOKEN(state, data){
        console.log("userOrders数据已存放到仓库。")
        state.userOrders = data;
    },
    SETUSERBALANCE(state, data){
        state.userBalance = data;
        window.sessionStorage.setItem('USERBALANCE', data);
    }
};
const getters = {};

export default {
    state,
    actions,
    mutations,
    getters
}